package com.example.yls.demoa;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import java.util.List;

public class StuAdapter extends RecyclerView.Adapter<StuViewHolder>{

    private List<Student> studentList;
    private Context context;
    private IOnChange onChange;

    public StuAdapter(List<Student> studentList, Context context, IOnChange onChange){
        this.studentList = studentList;
        this.context = context;
        this.onChange = onChange;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }

    @Override
    public StuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item,
                parent,false);

        StuViewHolder stuViewHolder = new StuViewHolder(view);
        return stuViewHolder;
    }

    @Override
    public void onBindViewHolder(StuViewHolder holder, final int position) {
        final Student s = studentList.get(position);
        holder.name.setText(s.getName());
        holder.age.setText(String.valueOf(s.getAge()));
        holder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onChange.del(position);
            }
        });

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onChange.edit(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return studentList.size();
    }

}
